package cvut.fit.dpo.arithmetic;

import java.util.Iterator;

import cvut.fit.dpo.arithmetic.iterator.InOrderIterator;
import cvut.fit.dpo.arithmetic.iterator.PostOrderIterator;
import cvut.fit.dpo.arithmetic.parsing.ExpressionElement;

public class ArithmeticExpression implements IComponent
{
	private BinaryOperator operator = null;
	private IComponent component;
	//public Stack<ExpressionElement> iterationStack;
	
	public ArithmeticExpression(BinaryOperator op){
		this.operator = op;
		//this.iterationStack = new Stack<ExpressionElement>();
	}
	
	public ArithmeticExpression(IComponent comp){
		this.component = comp;
		//this.iterationStack = new Stack<ExpressionElement>();
	}
	
	@Override
	public Integer evaluate()
	{
		if(this.operator != null) return operator.evaluate();
		else return this.component.evaluate();
	}
	
	public BinaryOperator getOperator(){
		return this.operator;
	}
	
	public void setRoot(BinaryOperator root)
	{
		this.operator = root;
	}

	/**
	 * {@link #operator} field getter.
	 * 
	 * @deprecated Do not provide access to the inner representation
	 */
	@Deprecated
	public BinaryOperator getRoot()
	{
		
		//return operator;
		throw new UnsupportedOperationException("Depracted method.");
		
	}
	
	@Override
	public String toString(){
		if(this.operator != null) return this.operator.toString();
		else return this.component.toString();
	}
	
	public int getValue(){
		if(this.component != null)return this.component.evaluate();
		else return 0;
	}
	
	public Iterator<ExpressionElement> getInOrderIterator()
	{
		InOrderIterator iterator = new InOrderIterator(this);
		return iterator;
	}

	public Iterator<ExpressionElement> getPostOrderIterator()
	{
		PostOrderIterator iterator = new PostOrderIterator(this);
		return iterator;
	}

}